package com.glitter.player.javabasesemaphore;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.util.concurrent.Semaphore;

@SpringBootApplication
@Slf4j
public class JavaBaseSemaphoreApplication implements CommandLineRunner {

    public static void main(String[] args) {
        SpringApplication.run(JavaBaseSemaphoreApplication.class, args);
    }

    private Semaphore semaphore=new Semaphore(2);


    @Override
    public void run(String... args) throws Exception {
        Thread PLAYER_THREAD = new Thread(() -> print());
        Thread GLITTER_THREAD = new Thread(() -> print());
        Thread HAN_HAN_THREAD = new Thread(() -> print());
        PLAYER_THREAD.setName("PLAYER");
        GLITTER_THREAD.setName("GLITTER");
        HAN_HAN_THREAD.setName("HAN_HAN");

        PLAYER_THREAD.start();
        GLITTER_THREAD.start();
        HAN_HAN_THREAD.start();
        while (true){

        }

    }


    private void print() {
        try {
            semaphore.acquire();
            log.info(Thread.currentThread().getName() + "开始安检");
            Thread.sleep(10000l);
            log.info(Thread.currentThread().getName() + "安检完毕，进去了");
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            semaphore.release();
        }
    }
}
